using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace DailyTransaction
{
    public partial class frmLogin : Form
    {
        private BAO bao = new BAO();
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            string pass = txtPassword.Text != null ? txtPassword.Text.ToString() : "";
            string userID = txtUsername.Text != null ? txtUsername.Text.ToString() : "";
            string encodedPass = bao.EncodePassword(txtPassword.Text.Trim());
            bool isvaliduser = false;
            try
            {
                isvaliduser = bao.Check_Username_Pass(userID, encodedPass);
            }
            catch (Exception ex)
            {
                //MessageBox.Show(GlobalClass.errmsg);
                return;
            }
            if (isvaliduser == true)
            {
                DataTable tblUser = bao.getUser(userID, encodedPass);
                int role = int.Parse(tblUser.Rows[0]["ROLE"].ToString());
                string user = tblUser.Rows[0]["USERID"].ToString();
                //frmDailyChecking frmChecking = new frmDailyChecking();
                //frmChecking.role = role;
                //frmChecking.ShowDialog();
                GlobalClass.user = user;
                GlobalClass.role = role;
                GlobalClass.islogin = true;
                this.Close();
                //frmMain frmm = new frmMain();
                //frmm.Show();                
            }
            else
            {
                GlobalClass.islogin = false;
                MessageBox.Show(null,"Error username or password!","Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                txtPassword.Focus();
            }
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            GlobalClass.islogin = false;
            this.Close();
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin.Focus();
        }
    }
}